//
//  QYHoldingViewController.m
//  QingYue
//
//  Created by leilei on 2017/3/26.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYHoldingViewController.h"
#import "QYHoldingListBookViewController.h"
#import "QYHoldingCateListViewController.h"

static CGFloat kOffset = 15;   // 按钮间距
@interface QYHoldingViewController ()
/** 列表collectionView*/
@property (nonatomic,strong) UICollectionView* listCollectionView;

@property (strong, nonatomic) UIButton *renwenButton;  // 人文自然
@property (strong, nonatomic) UIButton *swButton;  // 生物
@property (strong, nonatomic) UIButton *historyButton;  // 历史
@property (strong, nonatomic) UIButton *farmButton;   // 农业
@property (strong, nonatomic) UIButton *storyButton;  // 小说
@property (strong, nonatomic) UIButton *meiwenButton;  // 美文
@property (strong, nonatomic) UIButton *shikanButton;  // 时刊
@property (strong, nonatomic) UIButton *scienceButton;  // 环境科学
@property (strong, nonatomic) UIButton *artButton;     // 艺术
@property (strong, nonatomic) UIButton *medicalButton; // 医学
@property (strong, nonatomic) UIButton *moreButton;    // 更多

@property (nonatomic, copy) NSArray *dataArr;

@end

@implementation QYHoldingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"馆藏";
    self.view.backgroundColor = RGB(242, 242, 242, 1);
    
    [self.view addSubview:self.renwenButton];
    [self.view addSubview:self.swButton];
    [self.view addSubview:self.historyButton];
    [self.view addSubview:self.farmButton];
    [self.view addSubview:self.storyButton];
    [self.view addSubview:self.meiwenButton];
    [self.view addSubview:self.shikanButton];
    [self.view addSubview:self.scienceButton];
    [self.view addSubview:self.artButton];
    [self.view addSubview:self.medicalButton];
    [self.view addSubview:self.moreButton];
    
    [self loadData];
    
}


- (void)btnClickAction:(UIButton *)sender {
    NSLog(@"%ld",sender.tag);
    
    if (sender.tag == 50010) {
        QYHoldingCateListViewController *hclVC =[[QYHoldingCateListViewController alloc] init];
        hclVC.title = @"分类";
        hclVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:hclVC animated:YES];
    } else {
        QYHoldingListBookViewController *hlbVC = [[QYHoldingListBookViewController alloc] init];
        hlbVC.holdingType = _dataArr[sender.tag-50000][@"type"];
        hlbVC.hidesBottomBarWhenPushed = YES;
        hlbVC.title = _dataArr[sender.tag-50000][@"name"];
        [self.navigationController pushViewController:hlbVC animated:YES];
    }
    
}

- (void)loadData {
    
    NSArray<UIButton *> *btnArrs = @[_renwenButton,_swButton,_historyButton,_farmButton,_storyButton,_meiwenButton,_shikanButton,_scienceButton,_artButton,_medicalButton];
    
    NSMutableArray *data = [NSMutableArray array];
    
    [[YCRequestDataService sharedManager] Post:@"GetHoldingType" parameters:nil success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            NSArray *holdingList = response[@"holdingList"];
            [holdingList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx == 10) {
                    *stop = YES;
                }
                [data addObject:obj];
            }];
        }
        
        [btnArrs enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.titleLabel.font = Font(14);
            if (idx == 2) obj.titleLabel.font = Font(10);
            [obj setTitle:data[idx][@"name"] forState:UIControlStateNormal];
            obj.tag = 50000+idx;
            [obj addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        }];
        
        _dataArr = [data copy];
        
    } failure:^{
        
    }];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    [self layoutSubviews];
    
}

#pragma mark - layout subviews 
- (void)layoutSubviews {
    
    CGFloat bigButtonWidth = (self.view.width - 3 * kOffset) / 2;
    CGFloat bigButtonHeight = (self.view.height - 6 * kOffset) / 5 * 2 - 20;
    CGFloat smallButtonWidth = ((self.view.width / 2 - 2 * kOffset) - 10) / 2;
    CGFloat smallButtonHeight = (self.view.height - 6 * kOffset) / 5 - 17;
    
    [_renwenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(kOffset);
        make.left.equalTo(self.view.mas_left).offset(kOffset);
        make.width.equalTo(@(bigButtonWidth));
        make.height.equalTo(@(bigButtonHeight));
    }];
    
    
    [_swButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(kOffset);
        make.left.equalTo(_renwenButton.mas_right).offset(kOffset);
        make.width.equalTo(@(smallButtonWidth));
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_historyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(kOffset);
        make.left.equalTo(_swButton.mas_right).offset(kOffset);
        make.width.equalTo(@(smallButtonWidth));
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_farmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_swButton.mas_bottom).offset(kOffset);
        make.left.equalTo(_renwenButton.mas_right).offset(kOffset);
        make.right.equalTo(self.view.mas_right).offset(-kOffset);
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_storyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_renwenButton.mas_bottom).offset(kOffset);
        make.left.equalTo(self.view.mas_left).offset(kOffset);
        make.width.equalTo(@(smallButtonWidth));
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_meiwenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_renwenButton.mas_bottom).offset(kOffset);
        make.left.equalTo(_storyButton.mas_right).offset(kOffset);
        make.width.equalTo(@(smallButtonWidth));
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_shikanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_renwenButton.mas_bottom).offset(kOffset);
        make.left.equalTo(_renwenButton.mas_right).offset(kOffset);
        make.right.equalTo(self.view.mas_right).offset(-kOffset);
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_scienceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_storyButton.mas_bottom).offset(kOffset);
        make.left.equalTo(self.view.mas_left).offset(kOffset);
        make.width.equalTo(@(bigButtonWidth));
        make.height.equalTo(@(bigButtonHeight));
    }];
    
    
    [_artButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scienceButton.mas_top);
        make.left.equalTo(_scienceButton.mas_right).offset(kOffset);
        make.width.equalTo(@(smallButtonWidth));
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_medicalButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scienceButton.mas_top);
        make.left.equalTo(_artButton.mas_right).offset(kOffset);
        make.width.equalTo(@(smallButtonWidth));
        make.height.equalTo(@(smallButtonHeight));
    }];
    
    [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_artButton.mas_bottom).offset(kOffset);
        make.left.equalTo(_renwenButton.mas_right).offset(kOffset);
        make.right.equalTo(self.view.mas_right).offset(-kOffset);
        make.height.equalTo(@(smallButtonHeight));
    }];
}

#pragma mark - lazyloading
- (UIButton *)renwenButton {
    if (!_renwenButton) {
        _renwenButton = [[UIButton alloc] init];
        _renwenButton.backgroundColor = [UIColor blueColor];
    }
    
    return _renwenButton;
}

-(UIButton *)swButton {
    if (!_swButton) {
        _swButton = [[UIButton alloc] init];
        _swButton.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:.7];
    }
    
    return _swButton;
}

- (UIButton *)historyButton {
    if (!_historyButton) {
        _historyButton = [[UIButton alloc] init];
        _historyButton.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.8];
    }
    
    return _historyButton;
}

- (UIButton *)farmButton {
    if (!_farmButton) {
        _farmButton = [[UIButton alloc] init];
        _farmButton.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:.8];
    }
    
    return _farmButton;
}

- (UIButton *)storyButton {
    if (!_storyButton) {
        _storyButton = [[UIButton alloc] init];
        _storyButton.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.8];
    }
    
    return _storyButton;
}

- (UIButton *)meiwenButton {
    if (!_meiwenButton) {
        _meiwenButton = [[UIButton alloc] init];
        _meiwenButton.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:.8];
    }
    
    return _meiwenButton;
}

- (UIButton *)shikanButton {
    if (!_shikanButton) {
        _shikanButton = [[UIButton alloc] init];
        _shikanButton.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:.8];
    }
    
    return _shikanButton;
}

- (UIButton *)scienceButton {
    if (!_scienceButton) {
        _scienceButton = [[UIButton alloc] init];
        _scienceButton.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.8];
    }
    
    return _scienceButton;
}

- (UIButton *)artButton {
    if (!_artButton) {
        _artButton = [[UIButton alloc] init];
        _artButton.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.9];
    }
    
    return _artButton;
}

- (UIButton *)medicalButton {
    if (!_medicalButton) {
        _medicalButton = [[UIButton alloc] init];
        _medicalButton.backgroundColor = [[UIColor brownColor] colorWithAlphaComponent:.8];
    }
    
    return _medicalButton;
}

- (UIButton *)moreButton {
    if (!_moreButton) {
        _moreButton = [[UIButton alloc] init];
        _moreButton.backgroundColor = [UIColor orangeColor];
        [_moreButton setTitle:@"更多" forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _moreButton.tag = 50010;
    }
    
    return _moreButton;
}

#pragma mark - lazy load
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
